package connectFMS
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;

	public class FMSConnection extends Sprite
	{
		public static const CONNECTED:String="connected";
		private var ncUrl:String="rtmp://localhost/test";
		private var nc:NetConnection;
		private var myResponder:Responder;
		
		public function FMSConnection()
		{
			myResponder=new Responder(onReply);
			connect(ncUrl);
		}
		/* comment function */
		private function connect(ncUrl:String):void
		{
			nc=new NetConnection();										
			nc.connect(ncUrl);
			nc.client=this;
			
			nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
			nc.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityEvent);
		}
		/* comment function */
		private function onNetStatus(evt:NetStatusEvent):void
		{
			trace("status receive: "+evt.info.code);	
			switch(evt.info.code)
			{
				case "NetConnection.Connect.Success":
				{
					trace("Connect to server");	
					callServerSideF();
					//dispatchEvent(new Event(FMSConnection.CONNECTED,true));
					break;
				}
				case "NetConnection.Connect.Rejected":
				{
					trace("Connect rejected");
					break;
				}
				case "NetConnection.Connect.Closed":
				{
					trace("Connect closed");
					break;
				}	
			}
		}
		/* comment function */
		private function onIOError(evt:IOErrorEvent):void
		{
			throw new Error("IO Error on connection.");
		}
		/* comment function */
		private function onSecurityEvent(evt:SecurityErrorEvent):void
		{
			throw new Error("Security Error on connection.");
		}
		public function callServerSideF():void
		{
			//Call a server side function written on an .ASC file
			nc.call("callFromClient", myResponder, "Server .How are you?");
		}
		
		public function calledByServerSide(msg:String):void
		{
			//Function called by an .ASC file
			trace("Server say: "+msg);
		}
		
		// Responder function for nc.call() 
		private function onReply(result:Object):void 
		{
			trace("Client have to say: " + result);
		}
	}
	/* End Class */
}